package string;

import org.junit.Test;

/**
 * @Auther lujieni
 * @Date 2020/4/10
 */
public class TestString {

    @Test
    public void test(){
        String eventDescZh = String.format("尾程单号生成(供应商名称: UBI; 物流单号: %s)", "1221122");
        System.out.println(eventDescZh);
    }

    @Test
    public void test2(){
        boolean contains = "510000-510400-510403".contains("510000-510400-");
        System.out.println(contains);
    }

    @Test
    public void test3(){
        String n=null;
        String result = "122222-222221-null".replace("null", "");
        System.out.println(""+n);
    }

    @Test
    public void test4(){
        String str1="droid";
        String str2="droid";
        System.out.println(str1 == str2);
        String str3 = new String("droid");
        System.out.println(str1==str3);

        /*
            调用intern后，首先检查字符串常量池中是否有该对象的引用，
            如果存在,则将这个引用返回给变量,否则将引用加入并返回给变量。
         */
        String str4=str3.intern();

        System.out.println(str1 == str4);

    }

    @Test
    public void test5(){
        String str1="ab";
        /*
          由于+左右都是显式的字符常量,所以加号操作符没有申请新的String变量,
          而是指向常量池中原有的”ab”. 如果常量池中并没有”ab”, 那么加号操作符就会在常量池中添加”ab”
         */
        String str2="a"+"b";
        System.out.println(str1 == str2);
    }

    static String  base = "string";


    @Test
    public void test6(){
        String str = base+base;//str相当于new出来的
        System.out.println(str.intern() == "stringstring");
    }

    /**
     * 1.sb将值赋值给content后就没有关系了,即使sb变动了content仍旧不变
     * 2.a将值赋值给b之后就没有关系了,即使a变动了,b仍旧不变
     */
    @Test
    public void test7(){
        StringBuilder sb = new StringBuilder();
        String content = sb.append("12333").toString();
        sb.delete(0,sb.length());
        System.out.println(content); //12333
        System.out.println(sb.toString().length()); //0

        String a = "123";
        String b = a;
        a = "456";
        System.out.println(b); //123
    }

    /**
     *  1.deleteCharAt会删除指定位置的字符,但如果后面还有值的话后面的会取代前面的
     *  2.百分之99以上的场景使用StringBuilder即可
     */
    @Test
    public void test08(){
        StringBuilder sb = new StringBuilder("12,13,14");
        sb.deleteCharAt(0);
        System.out.println(sb); //2,13,14
    }

    /**
     * @Description: String#substring 和 StringBuilder#substring的结果必须要用返回值来接收!!!
     * @param
     * @return:
     * @Author: lujieni
     * @Date: 2021/5/31
     */
    @Test
    public void test09(){
       String s = "a,b,c";
       String s2 = s.substring(s.length() - 1);
       System.out.println(s); //a,b,c
       System.out.println(s2); //c
    }

    @Test
    public void test10(){
        String result = String.format("%08d", 1123);
        System.out.println(result);
    }

    @Test
    public void test11(){
        String uploadPath = String.format("/%s/%s", "ubi", "2023-01-30");
        System.out.println(uploadPath);

        String fileName = String.format("%s.pdf", "hello");
        System.out.println(fileName);
    }

    @Test
    public void test12(){
        String s = "\"你好\"";
        System.out.println(s);
    }

    /**
     * java中\是转义字符,所以正则中 \d -> \\d
     */
    @Test
    public void test13(){
        System.out.println("\"");
    }


}
